package com.yonyou.rabbitmq.provider.basic.topics;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yonyou.rabbitmq.utils.RabbitmqUtils;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) {

        Connection connection = null;
        Channel channel = null;

        try {
            //获取连接
            connection = RabbitmqUtils.getConnection();
            if (connection != null) {
                //获取通道
                channel = connection.createChannel();
                //声明交换机及交换机类型
                channel.exchangeDeclare("topics","topic");

                //发布消息
                String routingKey = "user.save";
                String message = "hello topic,routingKey:"+routingKey;
                channel.basicPublish("topics",routingKey,null,message.getBytes());

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("生产者发布消息完成......");
            //释放资源
            RabbitmqUtils.close(channel,connection);
        }
    }
}